/*   1:    */package com.linkage.bss.commons.util;
/*   2:    */
/*   3:    */import java.io.BufferedReader;
/*   4:    */import java.io.FileReader;
/*   5:    */import java.io.IOException;
/*   6:    */import org.springframework.context.ApplicationContext;
/*   7:    */import org.springframework.context.support.ClassPathXmlApplicationContext;
/*   8:    */import org.springframework.core.io.ClassPathResource;
/*   9:    */
/*  21:    */public class MockUtil
/*  22:    */{
/*  23: 23 */  private static String JAVAFILEPATH = "JavaMock.xml";
/*  24: 24 */  private static String JSONFILEPATH = "Mock.json";
/*  25: 25 */  private static String XMLFILEPATH = "XmlMock.xml";
/*  26: 26 */  private static String TXTFILEPATH = "Mock.txt";
/*  27:    */  
/*  28: 28 */  public static int MOCK_JAVA_BEAN_TYPE = 1;
/*  29: 29 */  public static int MOCK_PLAIN_DATA_TYPE = 2;
/*  30:    */  
/*  31: 31 */  private static String PIX = "/mockdata/";
/*  32:    */  
/*  40:    */  public static Object getJavaObject(String name)
/*  41:    */  {
/*  42: 42 */    ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { comboFileName(name, 1) });
/*  43: 43 */    return ctx.getBean(name);
/*  44:    */  }
/*  45:    */  
/*  54:    */  public static Object getJavaObjectById(String name, String beanId)
/*  55:    */  {
/*  56: 56 */    ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { comboFileName(name, 1) });
/*  57: 57 */    return ctx.getBean(beanId);
/*  58:    */  }
/*  59:    */  
/*  68:    */  public static Object getJavaObjectByPaths(String[] filePaths, String beanId)
/*  69:    */  {
/*  70: 70 */    ApplicationContext ctx = new ClassPathXmlApplicationContext(filePaths);
/*  71: 71 */    return ctx.getBean(beanId);
/*  72:    */  }
/*  73:    */  
/*  80:    */  public static String getMockDataByPath(String filePath)
/*  81:    */  {
/*  82: 82 */    return getContextFromFile(filePath);
/*  83:    */  }
/*  84:    */  
/*  85:    */  public static String getJsonObject(String name) {
/*  86: 86 */    return getContextFromFile(comboFileName(name, 2));
/*  87:    */  }
/*  88:    */  
/*  89:    */  public static String getXmlObject(String name) {
/*  90: 90 */    return getContextFromFile(comboFileName(name, 3));
/*  91:    */  }
/*  92:    */  
/*  93:    */  public static String getTextObject(String name) {
/*  94: 94 */    return getContextFromFile(comboFileName(name, 4));
/*  95:    */  }
/*  96:    */  
/*  97:    */  private static String getContextFromFile(String filePath) {
/*  98: 98 */    BufferedReader br = null;
/*  99:    */    try {
/* 100:100 */      br = new BufferedReader(new FileReader(new ClassPathResource(filePath).getFile()));
/* 101:101 */      StringBuffer sb = new StringBuffer();
/* 102:102 */      String line = null;
/* 103:103 */      while ((line = br.readLine()) != null) {
/* 104:104 */        sb.append(line).append(System.getProperty("line.separator"));
/* 105:    */      }
/* 106:106 */      return sb.toString();
/* 107:    */    } catch (IOException e) {
/* 108:108 */      throw new MockUtilException("查找文件filePath失败，不能获取Mock对象！", e);
/* 109:    */    } finally {
/* 110:110 */      if (br != null)
/* 111:    */        try {
/* 112:112 */          br.close();
/* 113:    */        } catch (IOException e) {
/* 114:114 */          e.printStackTrace();
/* 115:    */        }
/* 116:    */    }
/* 117:    */  }
/* 118:    */  
/* 119:    */  private static String comboFileName(String name, int i) {
/* 120:120 */    String pix = PIX + name + "/" + name;
/* 121:121 */    switch (i) {
/* 122:    */    case 1: 
/* 123:123 */      return pix + JAVAFILEPATH;
/* 124:    */    case 2: 
/* 125:125 */      return pix + JSONFILEPATH;
/* 126:    */    case 3: 
/* 127:127 */      return pix + XMLFILEPATH;
/* 128:    */    case 4: 
/* 129:129 */      return pix + TXTFILEPATH;
/* 130:    */    }
/* 131:131 */    return null;
/* 132:    */  }
/* 133:    */}


/* Location:           E:\workspace\sungl111\workspace_promotion\BizHallConfig\WebRoot\WEB-INF\lib\bss.commons-0.1.jar
 * Qualified Name:     com.linkage.bss.commons.util.MockUtil
 * JD-Core Version:    0.7.0-SNAPSHOT-20130630
 */